<?php
/*
 * 事件管理类
 */
class events 
{
	/*
	 * 事件通知
	 *
	 * @param	int		$event : 事件ID值, 来自Kohana::config('events.X')
	 * @param	object  $obj   : 产生该事件的类
	 */
	public static function notify ($event, $obj)
	{
		if (is_array($event))
		{
			foreach ($event as $e)
				self::notify($e, $obj);

			return;
		}

		$handler = self::factory ($event);

		if (!empty($handler))
		{
			if (is_object($handler))
			{
				try {
					$handler->handle($event, $obj);
				}
				catch (Exception $e)
				{
					//
				}
			}
			else if (is_array($handler))
			{
				foreach ($handler as $h)
					$h->handle($event, $obj);
			}

		} // !empty

		//@EventsMQ::handle($event, $obj);
	}

	/*
	 * 根据不同的事件，生产相应的事件处理器
	 * 处理器会以阻塞，或者非阻塞的方式运行(异步处理)
	 */
	private static function factory ($event)
	{
		switch ($event) 
		{
			case Kohana::config('events.phone400.addgroup'):
			case Kohana::config('events.phone400.editphone'):
				return Phone400::event_handler($event);
				break;
			case Kohana::config('events.trade.notify.pay_order_success'):
				return Shop::event_handler ($event);
				break;
			case Kohana::config('events.trade.notify.wait_buyer_pay'):
			case Kohana::config('events.order.refund_success'):
			case Kohana::config('events.order.alipay_set_discount'):
				return Order::event_handler ($event);
				break;

			case Kohana::config('events.trade.notify.wait_seller_send_goods'):
				return array(
					Order::event_handler ($event),
					Bill::event_handler ($event),
					Trade_Pbo::event_handler($event),
				);
				break;

			case Kohana::config('events.trade.notify.wait_buyer_confirm_goods'):
			case Kohana::config('events.trade.notify.trade_finished'):
			case Kohana::config('events.trade.notify.trade_closed'):
				return array(
					Order::event_handler ($event),
					Bill::event_handler ($event),
				);
				break;

			case Kohana::config('events.trade.notify.wait_seller_agree'):
			case Kohana::config('events.trade.notify.refund_closed'):
			case Kohana::config('events.trade.notify.seller_refuse_buyer'):
			case Kohana::config('events.trade.notify.wait_buyer_return_goods'):
			case Kohana::config('events.trade.notify.wait_seller_confirm_goods'):
				return Trade_Refund::event_handler ($event);
				break;

			case Kohana::config('events.trade.notify.refund_success'):
				return array(
					Trade_Refund::event_handler ($event),
					Bill::event_handler ($event),
				);
				break;

			case Kohana::config('events.user.question_answered') :
			case Kohana::config('events.user.report_handled') :
			case Kohana::config('events.user.complaint_handled') :
			case Kohana::config('events.user.created') :
			case Kohana::config('events.pbo.success') :
				return User::event_handler ($event);
				break;

			case Kohana::config('events.comments.type.member_to_merchant'):
			case Kohana::config('events.comments.type.merchant_to_member'):
				return Order::event_handler ($event);
				break;

			case Kohana::config('events.trade.notify.order_invoice'):
				return Order::event_handler ($event);
				break;
				
			case Kohana::config('events.myfavorite.goods'):
			case Kohana::config('events.myfavorite.shop'):
				return Model_My_Myfavorite::event_handler($event);
				break;
				
			case Kohana::config('events.browse.today_first_goods'):
				return Model_My_Browse::event_handler($event);
				break;
				
			case Kohana::config('events.order.create_valid_order'):
				return array(
					shop::event_handler($event),
// 					Events_Order::instance($event),
// 					Snapshot::event_handler($event),
// 					Message_Order::event_handler ($event),
				);
				break;

			case Kohana::config('events.goods.after_update') :
				return array(
					Snapshot::event_handler($event),
					Goods::event_handler($event),
					Topic::event_handler($event),
				);
				break;

			case Kohana::config('events.goods.created'):
				return array(
					Snapshot::event_handler($event),
					Goods::event_handler ($event),
				);
				break;

			case Kohana::config('events.goods.purchase') :
				return Goods::event_handler ($event);
				break;
				
			case Kohana::config('events.ccategory.remove') :
				return Model_CCategory::event_handler($event);
				break;

			case Kohana::config('events.respool.homepage_update'):
				return array(
						Home::event_handler($event),
						Cooperate::event_handler($event),
				);
				break;
			
			case Kohana::config('events.respool.homepage_update'):
			case Kohana::config('events.homepage.update') :
				return Home::event_handler($event);
				break;

			case Kohana::config('events.shop.config_plan_update'):
			case Kohana::config('events.shop.update_baseinfo'):
			case Kohana::config('events.shop.update_audit'):
				return Shop::event_handler($event);
				break;
			default : 
				return NULL;
				break;
		}
	}
}
